#!/bin/bash

cat << EOF
                                           _ __       __
   __  ____  ______ ___     ______      __(_) /______/ /_
  / / / / / / / __ \`__ \   / ___/ | /| / / / __/ ___/ __ \\
 / /_/ / /_/ / / / / / /  (__  )| |/ |/ / / /_/ /__/ / / /
 \__, /\__,_/_/ /_/ /_/  /____/ |__/|__/_/\__/\___/_/ /_/
/____/

EOF

# yum 源列表
declare -A yumlist
yumlist["ali"]="http://mirrors.aliyun.com/repo/Centos-7.repo"
yumlist["tencent"]="https://mirrors.cloud.tencent.com/repo/centos7_base.repo"
yumlist["huawei"]="https://repo.huaweicloud.com/repository/conf/CentOS-7-anon.repo"
yumlist["163"]="https://mirrors.163.com/.help/CentOS7-Base-163.repo"

echo '请选择yum源：'
index=1
declare -A tmpmapping
for firm in "${!yumlist[@]}"; do
  echo [$index] $firm: ${yumlist[$firm]}
  tmpmapping[$index]=$firm
  : $((index++))
done

while true
do
  read selected
  firm=${tmpmapping[$selected]}
  if [[ ${yumlist[$firm]+exists} == 'exists' ]]; then
    break
  else
    echo '选择错误。'
  fi
done

echo '备份当前yum仓库文件'
current_date=$(date +%Y%m%d-%H%M%S)
mkdir /etc/yum.repos.d/bak_$current_date
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak_$current_date

echo '下载仓库文件：'${yumlist[$firm]}
cd /etc/yum.repos.d/
curl -O ${yumlist[$firm]}
yum clean all
yum makecache
yum repolist
